﻿using Century.Model;
using Century.Model.CMS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Century.Business.CMS
{
    public class SecurityService : BSOBase<ActionModule, CenturyDbContext>, ISecurityService
    {
        #region [ Private Mewhod(s) ]

        #endregion

        #region [ public Mewhod(s) ]

        public List<ActionModule> GetRoleActionModules(string role)
        {
            using (Context = new CenturyDbContext())
            {
                return GetList(c => c.ActionModuleRoles.Any(a => a.RoleName == role));
            }
        }

        public List<ActionModule> GetAllActionModules()
        {
            using (Context = new CenturyDbContext())
            {
                return GetAllIncluding(c => c.ActionModuleRoles).ToList();
            }
        }

        public ActionModule GetActionModule(int actionModuleId)
        {
            using (Context = new CenturyDbContext())
            {
                return Find(c => c.Id == actionModuleId, "ActionModuleRoles");
            }
        }

        public void AddActionModuleRole(string role, int actionModuleId)
        {
            using (Context = new CenturyDbContext())
            {
                Context.ActionModuleRoles.Add(new ActionModuleRole { RoleName = role, ActionModuleId = actionModuleId });
                Context.SaveChanges();
            }
        }

        public void RemveActionModuleRole(string role, int actionModuleId)
        {
            using (Context = new CenturyDbContext())
            {
                ActionModuleRole actionModuleRole = Context.ActionModuleRoles.FirstOrDefault(c => c.ActionModuleId == actionModuleId && c.RoleName == role);
                if (actionModuleRole != null)
                {
                    Context.ActionModuleRoles.Remove(actionModuleRole);
                    Context.SaveChanges();
                }
            }
        }
       
        public void OnLoginSuccess()
        {
           
        }
        #endregion

    }
}
